package com.fr.base.print;

import com.fr.report.PageSet;
import com.fr.report.ReportPage;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.print.DocFlavor.INPUT_STREAM;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;

public class PrintUtils
{
  public static void print(PageSet paramPageSet, boolean paramBoolean)
    throws PrinterException
  {
    print(paramPageSet, paramBoolean, null);
  }

  public static void print(PageSet paramPageSet)
    throws PrinterException
  {
    PrintService localPrintService = null;
    if (localPrintService == null)
      localPrintService = PrintServiceLookup.lookupDefaultPrintService();
    PrinterJob localPrinterJob = PrinterJob.getPrinterJob();
    try
    {
      localPrinterJob.setPrintService(localPrintService);
    }
    catch (PrinterException localPrinterException1)
    {
      System.out.println(localPrinterException1);
    }
    Book localBook = new Book();
    PageFormat localPageFormat1 = localPrinterJob.defaultPage();
    PageFormat localPageFormat2 = null;
    for (int i = 0; i < paramPageSet.size(); ++i)
    {
      ReportPage localReportPage = paramPageSet.getPage(i);
      if (localReportPage == null)
        break;
      localPageFormat2 = localReportPage.getPageFormat(localPageFormat1);
      localBook.append(localReportPage, localPageFormat2);
    }
    localPrinterJob.setPageable(localBook);
    boolean bool = localPrinterJob.printDialog();
    if (!(bool))
      return;
    try
    {
      localPrinterJob.print();
    }
    catch (PrinterException localPrinterException2)
    {
      System.out.println(localPrinterException2);
    }
  }

  public static void print(PageSet paramPageSet, boolean paramBoolean, String paramString)
    throws PrinterException
  {
    print(paramPageSet, paramBoolean, paramString, 0);
  }

  public static void print(PageSet paramPageSet, boolean paramBoolean, String paramString, int paramInt)
    throws PrinterException
  {
    int i;
    PrintService localPrintService = null;
    if (paramString != null)
    {
      localObject = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, null);
      for (i = 0; i < localObject.length; ++i)
        if (paramString.equals(localObject[i].getName()))
        {
          localPrintService = localObject[i];
          break;
        }
    }
    if (localPrintService == null)
      localPrintService = PrintServiceLookup.lookupDefaultPrintService();
    Object localObject = PrinterJob.getPrinterJob();
    if (localPrintService != null)
      ((PrinterJob)localObject).setPrintService(localPrintService);
    PageFormat localPageFormat1 = ((PrinterJob)localObject).defaultPage();
    PageFormat localPageFormat2 = null;
    Book localBook = new Book();
    int j = 0;
    for (int k = 0; k < paramPageSet.size(); ++k)
    {
      ReportPage localReportPage = paramPageSet.getPage(k);
      if (localReportPage == null)
        break;
      if (j < paramInt)
      {
        ++j;
      }
      else
      {
        localPageFormat2 = localReportPage.getPageFormat(localPageFormat1);
        localPageFormat2 = ((PrinterJob)localObject).validatePage(localPageFormat2);
        localBook.append(localReportPage, localPageFormat2);
      }
    }
    ((PrinterJob)localObject).setPageable(localBook);
    if ((paramBoolean) || (paramString == null))
    {
      boolean bool = ((PrinterJob)localObject).printDialog();
      if (!(bool))
        return;
    }
    ((PrinterJob)localObject).print();
  }

  public static String[] getSystemPrinterNameArray()
  {
    ArrayList localArrayList = new ArrayList();
    PrintService[] arrayOfPrintService = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, new HashPrintRequestAttributeSet());
    for (int i = 0; i < arrayOfPrintService.length; ++i)
      localArrayList.add(arrayOfPrintService[i].getName());
    return ((String[])localArrayList.toArray(new String[localArrayList.size()]));
  }
}